<template>
	<view class="mainpadding">
		<uni-data-picker @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区"
			v-model="address.county" class="zdy">
		<view class="flexbetween xiahuaxian">
			<view class="flexleft">
				<view class="xiaohei sanshier  fonweight kuandu">地址</view>
					<input class="" type="text" placeholder="请选择地区" v-model="area" disabled="">
			</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		</uni-data-picker>
		<view class="flexleft xiahuaxian margin_top">
			<view class="xiaohei sanshier  fonweight kuandu">门牌号</view>
			<input type="text" placeholder="例：1单元101" v-model="addressdetail">
		</view>
		<view class="flexleft xiahuaxian margin_top">
			<view class="xiaohei sanshier  fonweight kuandu">联系人</view>
			<input type="text" placeholder="请填写联系人姓名" v-model="name">
		</view>
		<view class="flexleft xiahuaxian margin_top">
			<view class="xiaohei sanshier  fonweight kuandu">手机号</view>
			<input type="text" placeholder="请填写联系人手机号" v-model="mobile">
		</view>
		<view class="flexbetween margin_top">
			<view class="xiaohei sanshier  fonweight ">设置为默认地址</view>
			<u-switch v-model="value11" @change="change" activeColor="#FF9341"></u-switch>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="addaddress">保存地址</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "../static/city/city.js"
	export default {
		data() {
			return {
				area: "",
				listid: "",
				status: "",
				addressdetail: "", //详细地址
				address: {
					province: "", //省
					city: "", //市
					county: "", //区
				},
				name: "", //姓名
				mobile: "", //收货人电话
				data: {},
				value11: false,
				items: citys.data,
			}
		},
		onLoad(options) {
			this.listid = options.id;
			if (this.listid) {
				uni.setNavigationBarTitle({
					title:"编辑收货地址"
				})
				this.dizhixq()
			} else {
				uni.setNavigationBarTitle({
					title:"新增收货地址"
				})
			}
		},
		methods: {
			isMobile(value) { //手机号校验
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			// 地址详情接口
			dizhixq() {
				let _this = this;
				httpRequest.request('/api/user/addressDetail', 'GET', {
					// 传参
					id: _this.listid
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.area = res.data.cityinfo
						this.address.province = res.data.province_id
						this.address.city = res.data.city_id
						this.address.county = res.data.area_id
						this.name = res.data.name
						this.mobile = res.data.mobile
						this.addressdetail = res.data.address_detail
						if (res.data.status == 1) { //判断是否默认按钮
							this.value11 = true
						} else {
							this.value11 = false
						}
					}
				})
			},
			addaddress() { //添加地址
				uni.showLoading({
					title: '请稍候...',
					mask: true
				})
				let _this = this;
				let mobile = _this.mobile;
				if (_this.name == '') {
					uni.hideLoading()
					httpRequest.toast('请输入收货人名字');
					return false;
				}
				if (_this.mobile == "") {
					uni.hideLoading()
					httpRequest.toast('请输入收货人手机号');
					return false;
				}
				if (!_this.isMobile(_this.mobile)) {
					uni.hideLoading()
					httpRequest.toast("手机号输入有误")
					return false
				}
				if (_this.area == false) {
					uni.hideLoading()
					httpRequest.toast('请选择地址');
					return false;
				}
				if (_this.addressdetail == false) {
					uni.hideLoading()
					httpRequest.toast('请输入详细地址');
					return false;
				}
				let data = {
					name: _this.name,
					mobile: _this.mobile,
					address_detail: _this.addressdetail,
					status: _this.value11 ? 1 : 0,
					province_id: _this.address.province,
					city_id: _this.address.city,
					area_id: _this.address.county,
				}
				let url = "/api/user/addAddress"
				if(this.listid){
					url = "/api/user/editAddress"
					this.$set(data,"id",this.listid)
				}
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			bindPickerChange(val) {
				let area = val.detail.value
				let area_text = [area[0].text, area[1].text, area[2].text];
				this.area = area_text.join("/")
				this.address.province = area[0].value;
				this.address.city = area[1].value;
				this.address.county = area[2].value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.zdy {
		font-size: 32rpx;
	}

	.kuandu {
		width: 130rpx;
	}
</style>